#include "mainwindowimpl.h"
#include <qwt_plot_grid.h>

#define YES 1
#define NO 0
#define SIGN(a) (a>0)?1:0

MainWindowImpl::MainWindowImpl( QWidget * parent, Qt::WFlags f) 
    : QMainWindow(parent, f)
{
    setupUi(this);

    /*!
		\fn Connection of signals
         */

    initConst();
    selM[0]=selMetC;  selM[1]=selMetT;  selM[2]=selMetR;  selM[3]=selMetS;
    selA[0]=selWaveC; selA[1]=selWaveT; selA[2]=selWaveR; selA[3]=selWaveS;
    for(int i=0; i<TabArea->count(); i++)
    {
        QObject::connect(selM[i], SIGNAL(currentIndexChanged(QString)), this, SLOT(initMet(QString)));
        QObject::connect(selA[i], SIGNAL(currentIndexChanged(QString)), this, SLOT(initWave(QString)));
    }
    QObject::connect(TabArea, SIGNAL(currentChanged(int)), this, SLOT(initType(int)));	// Labels

    QRadioButton* par[3]={paramQ1, paramQ2, paramQ3};
    for(int i=0; i<3; i++) QObject::connect(par[i], SIGNAL(clicked()), this, SLOT(setParams()));

    QObject::connect(but_printParam, SIGNAL(pressed()), this, SLOT(initCalc()));

    //    QObject::connect(spinDm, SIGNAL(valueChanged(int)), this, SLOT(initMet()));
    //    QObject::connect(spinDn, SIGNAL(valueChanged(int)), this, SLOT(initMet()));
    //	QObject::connect(but_diag, SIGNAL(pressed()), this, SLOT(clearDatabase()));	// Clear Database before evaluate
    QObject::connect(waveTable, SIGNAL(doubleClicked(QModelIndex)), this, SLOT(showAlert(QModelIndex)));	//Alert
    QObject::connect(actionCopyRecord, SIGNAL(triggered()), this, SLOT(copyRecord()));	//Copy cells
    QObject::connect(actionCopyDatabase, SIGNAL(triggered()), this, SLOT(copyDatabase()));	//Copy Database
    QObject::connect(actionClear, SIGNAL(triggered()), this, SLOT(clearDatabase()));	//Clear Database
    QObject::connect(actionSave, SIGNAL(triggered()), this, SLOT(save()));	//Save components
    QObject::connect(actionAboutNefer, SIGNAL(triggered()), this, SLOT(about()));	//Save components

    /*!	\fn Init Database	*/
    QSqlQuery query;
    query.exec("DROP TABLE wave");
    QString queryText="id INTEGER PRIMARY KEY";
    for (int j=1;j<Base.count();j++) queryText += ", "+Base.value(j);
    query.exec("CREATE TABLE wave ("+queryText+")");

    /*!	\fn QTableView initialization	 */
    waveSqlModel = new QSqlTableModel(this);
    waveSqlModel->setTable("wave");
    waveSqlModel->select();
    waveTable->setModel(waveSqlModel);
    waveTable->resizeColumnsToContents();
    waveTable->addAction(actionCopyRecord);
    waveTable->addAction(actionCopyDatabase);
    waveTable->addAction(actionClear);
    waveTable->setColumnHidden(0,1);
    for (int j=12; j<32 ; j++) waveTable->setColumnHidden(j,1);
    waveSqlModel->select();

    /*! Graph initialization */
    QwtPlotGrid * grid[3];
    for (int i=0; i<3; i++) {
        grid[i] = new QwtPlotGrid();
        grid[i]->setPen(Qt::DotLine);
    }
    plotDiag->setCanvasBackground(Qt::white);
    grid[0]->attach(plotDiag);
    plotPhase->setCanvasBackground(Qt::white);
    grid[1]->attach(plotPhase);
    plotAFR->setCanvasBackground(Qt::white);
    grid[2]->attach(plotAFR);

    TabArea->setCurrentIndex(3);
}

void MainWindowImpl::initWave(QString cur)
{
    area=Area.indexOf(cur);
}

void MainWindowImpl::initMet(QString cur)
{
    method=Method.indexOf(cur);
    if(method==0 || method==1)
    {
        labelQ1->setText("R/L =");
        labelQ2->setText("Teta =");
        labelQ3->setText("Fi =");
        boxQ1->setValue(10);
        boxQ1->setMinimum(1);
        boxQ1->setMaximum(1000);
        boxQ2->setValue(15);
        boxQ3->setMinimum(-90);
        boxQ2->setMaximum(90);
        boxQ3->setValue(30);
        boxQ3->setMinimum(-360);
        boxQ3->setMaximum(360);
        boxQ3->setSuffix("");
    }
    if(method==1) boxQ1->setValue(100);
    if(method==2 || method==3 || method==4 || method==5)
    {
        labelQ1->setText("R/b =");
        labelQ2->setText("Fi =");
        labelQ3->setText("Z/L =");
        boxQ1->setValue(1);
        boxQ1->setMinimum(0);
        boxQ1->setMaximum(20);
        boxQ2->setValue(0);
        boxQ2->setMinimum(-360);
        boxQ2->setMaximum(360);
        boxQ3->setValue(0);
        boxQ3->setMinimum(0);
        boxQ3->setMaximum(1000);
        boxQ3->setSuffix(" mm");
    }
    if(method==4) groupDif->setVisible(YES);
    else groupDif->setVisible(NO);
    if(method==5) paramQ1->setChecked(YES);
    setParams();
}

void MainWindowImpl::setParams()
{
    QRadioButton* par[3]={paramQ1, paramQ2, paramQ3};
    QLabel* lab[3]={labelQ1, labelQ2, labelQ3};
    QDoubleSpinBox* box[3]={boxQ1, boxQ2, boxQ3};
    for(int i=0; i<3; i++)
    {
        if(par[i]->isChecked())
        {
            param=i;
            labelBegin->setText(lab[i]->text());
            labelEnd->setText(lab[i]->text());
            box[i]->setEnabled(NO);
        }
        else box[i]->setEnabled(YES);
    }
    if(param==0)
    {
        boxParam0->setValue(0);
        boxParam0->setMinimum(0);
        boxParamN->setMaximum(180);
        boxParamN->setValue(2);
    }
    else
    {
        boxParam0->setMinimum(0);
        boxParam0->setValue(0);
        boxParamN->setValue(90);
        boxParamN->setMaximum(360);
    }
}

void MainWindowImpl::initType(int num)
{
    curType=num;
    initMet (selM[num]->currentText());
    initWave(selA[num]->currentText());
}

void MainWindowImpl::initConst()
{
    Base[0]="id";
    Base[1]="realE"; Base[2]="imagE";	//AFR
    Base[3]="Eq1_re"; Base[4]="Eq1_im";	//R at scope&cyl - real&image
    Base[5]="Eq2_re"; Base[6]="Eq2_im";	//Teta at scope, Fi at cyl - real&image
    Base[7]="Eq3_re"; Base[8]="Eq3_im";	//Fi at scope, Z at cyl - real&image
    Base[9]="q1"; Base[10]="q2"; Base[11]="q3";	//coordinates of point
    Base[12]="rot_q1_re"; Base[13]="rot_q1_im"; Base[14]="rot_q2_re";	//rotor ingridients: real
    Base[15]="rot_q2_im"; Base[16]="rot_q3_re"; Base[17]="rot_q3_im";	//rotor ingridients: complex
    Base[18]="grad_q1_re"; Base[19]="grad_q1_im"; Base[20]="grad_q2_re";	//gradient ingridients: real
    Base[21]="grad_q2_im"; Base[22]="grad_q3_re"; Base[23]="grad_q3_im";	//rotor ingridients: complex
    Base[24]="j_q1_re"; Base[25]="j_q1_im"; Base[26]="j_q2_re";		//vector ingridients: real
    Base[27]="j_q2_im"; Base[28]="j_q3_re"; Base[29]="j_q3_im";	//rotor ingridients: complex*/
    Base[30]="flag_area"; Base[31]="flag_method";
    Base[32]="Amp"; Base[33]="Arg";

    Area << "Common-mode excitation";   // 0
    Area << "First area";   // 1
    Area << "Second area";   // 2
    Area << "Entire structure";   // 3
    Area << " ";   // 4
    Area << "Radial distribution of excitation";   // 5
    Area << "E01 mode";   // 6
    Area << "H01 mode";   // 7
    Area << "H11 mode";   // 8
    Area << "Strip";   // 9
    selWaveC->addItem(Area.at(6)); selWaveC->addItem(Area.at(7)); selWaveC->addItem(Area.at(8));
    selWaveC->addItem(Area.at(0)); selWaveC->addItem(Area.at(5));
    selWaveT->addItem(Area.at(1)); selWaveT->addItem(Area.at(2)); selWaveT->addItem(Area.at(3));
    selWaveR->addItem(Area.at(6)); selWaveR->addItem(Area.at(7)); selWaveR->addItem(Area.at(8));
    selWaveS->addItem(Area.at(9));
    selWaveC->setCurrentIndex(2); selWaveT->setCurrentIndex(2); selWaveR->setCurrentIndex(2);

    Method << "The spherical coordinate system";   // 0
    Method << "Fraunhofer approximation zone";   // 1
    Method << "Cilindrical coordinates";   // 2
    Method << "Factorization method (Vainshtein)";   // 3
    Method << "Solution of diffraction";   // 4
    Method << "Polynomials of Hermite-Gauss";   // 5
    selMetC->addItem(Method.at(0)); selMetC->addItem(Method.at(1)); selMetC->addItem(Method.at(2));
    selMetC->addItem(Method.at(3)); selMetC->addItem(Method.at(4));
    selMetT->addItem(Method.at(0)); selMetT->addItem(Method.at(2));
    selMetR->addItem(Method.at(3)); selMetR->addItem(Method.at(4));
    selMetS->addItem(Method.at(5));
    selMetC->setCurrentIndex(2); selMetT->setCurrentIndex(1); selMetR->setCurrentIndex(1);
}

void MainWindowImpl::showAlert(QModelIndex)
{
    /// @todo Alert messages alerting
    QMessageBox::information(0, qApp->tr("Editing not Allowed"),	//Message title, first of all is iconSet
                             qApp->tr("Editing not Allowed.\n"
                                      "Please copy data to another application"),	//Message text
                             QMessageBox::Close);				//ButtonRole define
}
